package cn.codesugar.material.design

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.codesugar.material.design.databinding.BearItemBinding
import com.bumptech.glide.Glide

class BearAdapter(private val context: Context, private val bearList: List<Bear>) :
    RecyclerView.Adapter<BearAdapter.ViewHolder>()  {

    inner class ViewHolder(b: BearItemBinding) : RecyclerView.ViewHolder(b.root) {
        val bearImage: ImageView = b.bearImage
        val bearName: TextView = b.bearName
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val b = BearItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(b)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bear = bearList[position]
        holder.bearName.text = bear.name
        Glide.with(context).load(bear.imageId).into(holder.bearImage)
        holder.itemView.setOnClickListener {
            val intent = Intent(context, BearActivity::class.java).apply {
                putExtra(BearActivity.BEAR_NAME, bear.name)
                putExtra(BearActivity.BEAR_IMAGE_ID, bear.imageId)
            }
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = bearList.size

}